################################################
#	Spry and Nunnally (2025)
# Replication Data for "Modern American Dilemma"
# Source
################################################

# Front Matter ------------------------------------------------------------

rm(list=ls())		# clear objects in memory

library(foreign) 
library(dplyr)
library(ggplot2)
library(gmodels)
library(stargazer)
library(questionr)
library(cfa)
library(lavaan)
library(reshape2)
library(jtools)
library(expss)
library(broom)
library(ggstance)
library(sjPlot)
library(sjmisc)
library(margins)
library(Hmisc)

set.seed(1234567)

setwd("/Users/amberspry/Dropbox/Research/Data/2016 CMPS")                      # Replace with local file path

# Data imported from 2016 CMPS

data <- read.dta("CMPS2016_zipdata.dta")          # Source data

# Dependent Variables -----------------------------------------------------

# Generate numeric DV - support for BLM
# 1=strongly support...5=strongly oppose
data$blm1 <- as.numeric(data$c228)

# Recode BLM support DV so that support takes on high values
# 1= strongly oppose... 5= strongly support
data$blm[data$blm1==1] <- 5
data$blm[data$blm1==2] <- 4
data$blm[data$blm1==3] <- 3
data$blm[data$blm1==4] <- 2
data$blm[data$blm1==5] <- 1

# Independent Variables ---------------------------------------------------

# Generate numeric IV - how important is being American
# 1=very important...4=not at all

data$amer1 <- as.numeric(data$c194)

# Recode so strong American ID takes high values

data$amer[data$amer1==1] <- 4
data$amer[data$amer1==2] <- 3
data$amer[data$amer1==3] <- 2
data$amer[data$amer1==4] <- 1


# Ideology variable recode 1 = conservative, 5 = liberal

data$ideol1 <- as.numeric(data$c31)

data$ideol[data$ideol1==1] <- 5
data$ideol[data$ideol1==2] <- 4
data$ideol[data$ideol1==3] <- 3
data$ideol[data$ideol1==4] <- 2
data$ideol[data$ideol1==5] <- 1
data$ideol[data$ideol1==6] <- NA


# Party ID variable recode

data$party3 <- as.numeric(data$c27)
data$party2 <- as.numeric(data$c25)


# Party ID treating Independent-leaners with partisans

data$party1[data$party2==1] <- 1      # Republicans
data$party1[data$party2==2] <- 2      # Democrats
data$party1[data$party3==1] <- 1      # Independents closer to R party
data$party1[data$party3==2] <- 2      # Independents closer to D party
data$party1[data$party2==3] <- NA      
data$party1[data$party2==4] <- NA


# Party ID with pure party identification

data$partyclean[data$c25=="Republican"]   <- 1  # Republicans
data$partyclean[data$c25=="Democrat"]     <- 2  # Democrats
data$partyclean[data$c25=="Independent"]  <- 3  # Independents
data$partyclean[data$c25=="Other party"]  <- NA


# Party Dummy Variables

data$rparty <- ifelse(data$partyclean == 1, 1, 0)   # Republican dummy
data$dparty <- ifelse(data$partyclean == 2, 1, 0)   # Democrat dummy
data$iparty <- ifelse(data$partyclean == 3, 1, 0)   # Independent dummy

data$dpartylean <- ifelse(data$party1 == 2, 1, 0)   # Democrat dummy including Independent leaners


# Protest effectiveness recode

data$protest1 <- as.numeric(data$c234)

data$protest[data$protest1==1] <- 4
data$protest[data$protest1==2] <- 3
data$protest[data$protest1==3] <- 2
data$protest[data$protest1==4] <- 1


# Black neighborhood estimate recode
data$bhood <- as.numeric(data$c349_2)


# Gender recode
data$gender <- as.numeric(data$s3)        # female =1, male =2, other = 3

# Female Dummy Variable
data$female <- ifelse(data$s3=="Female", 1, 0)   # female =1, else=2
table(data$female)

# Age recode
data$age1 <- as.numeric(data$s6)
data$age <- 2019-(data$age1 + 1900)


# Have you ever used camera on mobile phoone to record police misconduct?
# 1 = no never...2 = yes, once...3= yes, twice or more... 4 = No I don't have a mobile phone with camera (recode as 1)

data$cell1 <- as.numeric(data$c224)
data$cell <- data$cell1
data$cell[data$cell1==4] <- 1


# Efficacy : Public officials don't care much about what people like me think
# 1 = strongly agree (low efficacy = more support for BLM?) ... 5 = strongly disagree (high efficacy = low support for BLM)
# Recode so that strong agree = 5...strong disagree=1

data$efficacy1 <- as.numeric(data$c106)
data$efficacy[data$efficacy1==1] <- 5
data$efficacy[data$efficacy1==2] <- 4
data$efficacy[data$efficacy1==3] <- 3
data$efficacy[data$efficacy1==4] <- 2
data$efficacy[data$efficacy1==5] <- 1


# How often would you say public officials work to help [ans to s2]
# 1= all the time...5=never
# Recode so all the time=5...1=never

data$help1 <- as.numeric(data$c118)
data$help[data$help1==1] <- 5
data$help[data$help1==2] <- 4
data$help[data$help1==3] <- 3
data$help[data$help1==4] <- 2
data$help[data$help1==5] <- 1


# How often would you say [s2] have a say in how gov  handles important issues?
# 1= all the time...5=never

data$havesay <- as.numeric(data$c119)


# Linked fate valence

data$lfval1 <- as.numeric(data$c152)
# recode 1= negative 2=neutral 3=positive
data$lfval[data$lfval1==2] <- 1
data$lfval[data$lfval1==3] <- 2
data$lfval[data$lfval1==1] <- 3


# Linked fate
# Recode so no = 0, yes = 1

data$lf1 <-as.numeric(data$c150)
data$lf[data$lf1==2] <- 0
data$lf[data$lf1==1] <- 1
table(data$lf)


# Income

data$income <- as.numeric(data$c383)


# Dummy variables
data$dummy.black <- ifelse(data$s2_3 == 1, 1, 0)  # black race dummy
data$dummy.white <- ifelse(data$s2_1 == 1, 1, 0)  # white race dummy
data$dummy.female <- data$female                  # female dummy, female = 1, else = 0



# Factor Analysis ---------------------------------------------------------

# Values ------------------------------------------------------------------

# A. ATTRIBUTIONS OF DEMOCRATIC VALUES (VALUES)

# Most important issue

data$fa.corruption <- as.numeric(data$c1_9)     # corruption in government 
data$fa.racism <- as.numeric(data$c1_10)        # racism and race relations
data$fa.justice <- as.numeric(data$c1_14)       # criminal justice reform

# How effective are the following strategies for getting your voice heard by elected officials
# 1 = Very effective, 4 = not at all effective

data$fa.voting1 <- as.numeric(data$c233)            
data$fa.protest1 <- as.numeric(data$c234)           
data$fa.contact1 <- as.numeric(data$c235)            
data$fa.rioting1 <- as.numeric(data$c236)            

# Recode so that highly effective takes on high values
# 1= not at all effective... 5= very effective

data$fa.voting[data$fa.voting1==1] <- 4
data$fa.voting[data$fa.voting1==2] <- 3
data$fa.voting[data$fa.voting1==3] <- 2
data$fa.voting[data$fa.voting1==4] <- 1

data$fa.protest[data$fa.protest1==1] <- 4
data$fa.protest[data$fa.protest1==2] <- 3
data$fa.protest[data$fa.protest1==3] <- 2
data$fa.protest[data$fa.protest1==4] <- 1

data$fa.contact[data$fa.contact1==1] <- 4
data$fa.contact[data$fa.contact1==2] <- 3
data$fa.contact[data$fa.contact1==3] <- 2
data$fa.contact[data$fa.contact1==4] <- 1

data$fa.rioting[data$fa.rioting1==1] <- 4
data$fa.rioting[data$fa.rioting1==2] <- 3
data$fa.rioting[data$fa.rioting1==3] <- 2
data$fa.rioting[data$fa.rioting1==4] <- 1


# Practice ----------------------------------------------------------------

# B. PERCEPTIONS OF PRACTICE CONSISTENT WITH THEORY (PRACTICE)

# How much discrimination is there in the US today against the following groups?
# 1 = a great deal...4 = none at all, 5 = don't know

# Recode so that a great deal takes on high values
# 1 = none at all ... 4 = a great deal

data$fa.discwhite1 <- as.numeric(data$c243)
data$fa.discblack1 <- as.numeric(data$c244)

data$fa.discwhite[data$fa.discwhite1==1] <- 4
data$fa.discwhite[data$fa.discwhite1==2] <- 3
data$fa.discwhite[data$fa.discwhite1==3] <- 2
data$fa.discwhite[data$fa.discwhite1==4] <- 1
data$fa.discwhite[data$fa.discwhite1==5] <- NA       # 5 is "don't know" but may need to recode as NA

data$fa.discblack[data$fa.discblack1==1] <- 4
data$fa.discblack[data$fa.discblack1==2] <- 3
data$fa.discblack[data$fa.discblack1==3] <- 2
data$fa.discblack[data$fa.discblack1==4] <- 1
data$fa.discblack[data$fa.discblack1==5] <- NA       # 5 is "don't know" but may need to recode as NA


# Discrimination ----------------------------------------------------------

# C. PERSONAL EXPERIENCES WITH DEMOCRACY AND DISCRIMINATION (DISCRIM)

# How strongly do you feel like you belong in the US
# 1 = strongly belong...4 = not at all

# Recode so that strongly takes on high values
# 1 = not at all... 4 = strongly belong

data$fa.belong1 <- as.numeric(data$c107)

data$fa.belong[data$fa.belong1==1] <- 4
data$fa.belong[data$fa.belong1==2] <- 3
data$fa.belong[data$fa.belong1==3] <- 2
data$fa.belong[data$fa.belong1==4] <- 1


# How much do you feel like an outsider in the US
# 1 = strongly as outsider ... 4 = not at all outsider

data$fa.outsider <- as.numeric(data$c109)


# How often do you think other people try to exclude you from US society?
# 1 = always...4 = never

data$fa.exclude <- as.numeric(data$c110)


# Have you ever been treated unfairly or personally experienced discrimination?
# 1 = yes, 2= no

# Recode so that 1= no, 2=yes

data$fa.discrim1 <- as.numeric(data$c251)

data$fa.discrim[data$fa.discrim1==1] <- 0
data$fa.discrim[data$fa.discrim1==2] <- 1


# Expressions -------------------------------------------------------------

# D. EXPRESSIONS OF PATRIOTISM (EXPRESSIONS)

# How much is being American an important part of how you see yourself?
# 1 = very important... 4 = not at all important

# Recode so that very important takes high values
# 1 = not at all...4 = very

data$fa.american1 <- as.numeric(data$c194)

data$fa.american[data$fa.american1==1] <- 4
data$fa.american[data$fa.american1==2] <- 3
data$fa.american[data$fa.american1==3] <- 2
data$fa.american[data$fa.american1==4] <- 1



# Data Subsets ------------------------------------------------------------

# Data Race Subsets

sub.white1 <- subset(data, data$s2_1==1)
sub.hisp1 <- subset(data, data$s2_2==1)
sub.black1 <- subset(data, data$s2_3==1)
sub.asian1 <- subset(data, data$s2_4==1)
sub.bw1 <- subset(data, data$s2_1==1 | data$s2_3==1)

# Drop multiracial respondents in Black, White, and Black + White datasets

sub.white <- sub.white1[!(sub.white1$s2_1==1 & sub.white1$s2_3==1),]
sub.black <- sub.black1[!(sub.black1$s2_1==1 & sub.black1$s2_3==1),]
sub.bw <- sub.bw1[!(sub.bw1$s2_1==1 & sub.bw1$s2_3==1),]



# Create VARS

vars <- c("fa.voting", "fa.protest", "fa.contact", "fa.rioting", "fa.discwhite", "fa.discblack", "fa.belong", "fa.outsider", "fa.exclude")
vars.bw1 <- sub.bw[vars]
vars.black1 <- sub.black[vars]
vars.white1 <- sub.white[vars]

